/**
 * 
 */
package com.bloomingstars.sqm.orm;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 * The basic GenericDao interface with CRUD methods
 * @author Emmanuele Sordini
 *
 */
public interface GenericDao<T, PK extends Serializable>
{
    /**
     * Saves a new instance of type T into the DB
     * @param newInstance object to persist
     * @return the id of the new instance
     */
    public PK create(T newInstance);

    /**
     * Reads an object from the DB 
     * @param id id of the object
     * @return persistent object, or <code>null</code> 
     */
    public T read(PK id);

    /**
     * Updates an already existing object
     * @param transientObject that will overwrite the persistent one
     */
    public void update(T transientObject);

    /**
     * 
     * @param transientObject
     * @return
     */    
    public void saveOrUpdate(T transientObject);
    
    /**
     * Deletes a persistent object
     * @param persistentObject object to delete
     */
    public void delete(T persistentObject);
    
    /**
     * Deletes a persistent object
     * @param id persistent object id
     */
    public void delete(PK id);
        
    /**
     * Searches for objects in the DB through criteria/by example query 
     * @param example example of object
     * @return list of objects matching the search criteria
     */
    public List<T> findLike(T example);
    
    /** 
     * @param example Search criteria
     * @return <code>true</code> if at least one such object exists
     */
    public boolean existsLike(T example);
    
    /** 
     * @param id id of object
     * @return <code>true</code> if an object exists with that id
     */
    public boolean exists(PK id);
    
    /**
     * Custom restrictions on search criteria
     * @param criteria search criteria
     * @return constrained search criteria
     */
    public Criteria addRestrictionsToExample(Criteria criteria, T example);
    
    /**
     * 
     */
    public void resetSession(Session session);
    
    /**
     * 
     */
    public void resetSession();
    
    public List<PK> createList(List<T> list);
}
